package com.hexinfo.dmpro.data.transfer.common.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hexinfo.dmpro.common.base.PageResult;
import com.hexinfo.dmpro.common.utils.CommonConstants;
import com.hexinfo.dmpro.data.transfer.common.dao.DataTransferTableFilterMapper;
import com.hexinfo.dmpro.data.transfer.common.model.DataTransferTableFilter;
import com.hexinfo.dmpro.data.transfer.common.service.IDataTransferTableFilterService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DataTransferTableFilterService extends ServiceImpl<DataTransferTableFilterMapper, DataTransferTableFilter> implements IDataTransferTableFilterService {

    @Override
    public PageResult queryList(Page page, DataTransferTableFilter dataTransferTableFilter) {
        LambdaQueryWrapper<DataTransferTableFilter> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DataTransferTableFilter::getDel, CommonConstants.STATUS_NORMAL);
        if (StrUtil.isNotBlank(dataTransferTableFilter.getTransferType())) {
            wrapper.eq(DataTransferTableFilter::getTransferType, dataTransferTableFilter.getTransferType());
        }
        if (StrUtil.isNotBlank(dataTransferTableFilter.getDbName())){
            wrapper.like(DataTransferTableFilter::getDbName, dataTransferTableFilter.getDbName());
        }
        if (StrUtil.isNotBlank(dataTransferTableFilter.getTableName())){
            wrapper.like(DataTransferTableFilter::getTableName, dataTransferTableFilter.getTableName());
        }
        Page returnPage = this.baseMapper.selectPage(page, wrapper);
        return new PageResult(returnPage.getRecords(), returnPage.getTotal());
    }

    @Override
    public List<DataTransferTableFilter> queryListByType(String transferType) {
        LambdaQueryWrapper<DataTransferTableFilter> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DataTransferTableFilter::getTransferType, transferType);
        wrapper.eq(DataTransferTableFilter::getDel, CommonConstants.STATUS_NORMAL);
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public boolean create(DataTransferTableFilter dataTransferTableFilter) {
        if (checkIsExist(dataTransferTableFilter)) {
            throw new RuntimeException("数据传输表过滤器已存在");
        }
        return this.save(dataTransferTableFilter);
    }

    private boolean checkIsExist(DataTransferTableFilter dataTransferTableFilter) {
        LambdaQueryWrapper<DataTransferTableFilter> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DataTransferTableFilter::getTransferType, dataTransferTableFilter.getTransferType());
        wrapper.eq(DataTransferTableFilter::getDbName, dataTransferTableFilter.getDbName());
        wrapper.eq(DataTransferTableFilter::getTableName, dataTransferTableFilter.getTableName());
        wrapper.eq(DataTransferTableFilter::getDel, CommonConstants.STATUS_NORMAL);
        DataTransferTableFilter dbDataTransferTableFilter = this.baseMapper.selectOne(wrapper);
        return dbDataTransferTableFilter != null;
    }

}
